package cn.gzsf.sboot03.controller;


import cn.gzsf.sboot03.utils.DBUtils;
import cn.gzsf.sboot03.utils.entity.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//@Controller
@RestController //该注解会给该类中的每个方法添加@ResponseBody注解
public class EmpController {
    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        //获取连接的代码可以放在try()中，有编译器自动关闭连接
        try(Connection conn = DBUtils.getConn()) {
            String sql="insert into myemp values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setInt(2,emp.getSal());
            ps.setString(3,emp.getJob());
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "添加完成";
    }
    //查询员工列表
    @RequestMapping("/list")
    public String list(){
        List<Emp> emps = new ArrayList<>();//集合存员工对象
        try(Connection conn = DBUtils.getConn()){//获取连接
            String sql = "select * from myemp";
            Statement stat = conn.createStatement();//获取statement对象
            ResultSet rs = stat.executeQuery(sql);//执行
            while(rs.next()){//遍历所有员工信息
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int sal = rs.getInt(3);
                String job = rs.getString(4);
                emps.add(new Emp(id,name,job,sal));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String html = "<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><td>id</td><td>名字</td><td>工作</td><td>工资</td><td>操作</td></tr>";
        //添加多行员工信息
        for(Emp emp:emps){
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            html+="<td>"+emp.getSal()+"</td>";
            html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        try(Connection conn = DBUtils.getConn()) {
            String sql = "delete from myemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "<a href='/list'>返回员工列表</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        try (Connection conn = DBUtils.getConn()){
          String sql = "update myemp set name=?,job=?,sal=? where id=? " ;
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();//执行修改
        }catch (Exception e){
            e.printStackTrace();
        }
        return "<a href='/list'>返回员工列表</a>";
    }

}
